﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class ScreenSaver : Form
    {
        int i = 0, StartOrStop = 0, r, g, b, counter = 0;
        float t, x = 125, y = 100, v = 250, d = 200;
        Random R = new Random();
        Random G = new Random();
        Random B = new Random();

        Graphics gr; //建立繪圖介面類別g
        Pen p = new Pen(Color.Blue); //建立繪圖物件p(一枝筆顏色為藍色)
        Pen p1 = new Pen(Color.Yellow);
        Pen p2 = new Pen(Color.Red);
        //Pen p3 = new Pen(Color.Green);
        //Pen p4 = new Pen(Color.LightSteelBlue);
        Pen p5 = new Pen(Color.LightSteelBlue);

        public ScreenSaver()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "計時器測試"; //預設初始化時將button2顯示的文字為"畫一條線"
            timer1.Interval = 70;//設定timer的計數時間
            button1.Text = "啟動";//修改button1的文字
        }
        private void draw(int a)
        {
            {
                if (counter % 50 == 0)

                    t = t + 1;
                x = x + t;// -為往左跑 + 為往右跑
                y = y + t / 3;// - 好為往上跑 + 為向下跑 /號 是由於 只能 整數又會過大 故+上除號讓數字變小
                v = v + t;// -為往左跑 + 為往右跑
                d = d - t / 3;// - 好為往上跑 + 為向下跑 /號 是由於 只能 整數又會過大 故+上除號讓數字變小
            }

            int n;
            //for (n = 0; n < 2; n++)
            r = R.Next(0, 256);
            for (n = 0; n < 3; n++)
                g = G.Next(0, 256);
            b = B.Next(0, 256);

            Pen p3 = new Pen(Color.FromArgb(r, g, b));
            Pen p4 = new Pen(Color.FromArgb(r, g, b));

            gr = this.CreateGraphics(); //建立畫布g，將目前的表單當成畫布
            gr.Clear(Color.Black); //清除畫布g
            double c = Math.PI / 12.0;
            //float dx, dy;
            gr.DrawLine(p, x, y, x + 125 * (float)(Math.Sin(a * c)), y + 100 * (float)(Math.Cos(a * c)));
            gr.DrawLine(p1, x, y, x + 95 * (float)(Math.Cos(a * c)), y + 100 * (float)(Math.Sin(a * c)) + y * (float)(Math.Cos(a * c)));
            gr.DrawLine(p2, v, d, v + 75 * (float)(Math.Cos(a * c)), d + 100 * (float)(Math.Sin(a * c)) + d * (float)(Math.Cos(a * c)));
            gr.DrawLine(p3, v, d, v + 50 * (float)(Math.Sin(a * c)), d + 150 * (float)(Math.Cos(a * c)) + d * (float)(Math.Cos(a * c)));//右邊彩色逆時
            gr.DrawLine(p4, x, y, x + 25 * (float)(Math.Sin(a * c)), y + 100 * (float)(Math.Sin(a * c)) + y * (float)(Math.Cos(a * c)));
            gr.DrawLine(p5, v, d, v + 35 * (float)(Math.Sin(a * c)), d + 100 * (float)(Math.Sin(a * c)) + d * (float)(Math.Cos(a * c)));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartOrStop++;
            timer1.Enabled = !timer1.Enabled;
            if (StartOrStop % 2 == 1)
                button1.Text = "暫停";
            else
                button1.Text = "繼續";
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            draw(i); //將i的值放入draw方法中
            i++; //每次i加1
            counter++;
        }
    }
}